﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace cssColorList
{
    public partial class CssColor : UserControl
    {
        public CssColor(String color, String cssDir) {
            InitializeComponent();
            this.cssDir = cssDir;
            lblColor.BackColor = System.Drawing.ColorTranslator.FromHtml(color);
            currentColor = color;
            lblName.Text = color;
        }

        private String currentColor;
        private String cssDir;
        private void btnReplace_Click(object sender, EventArgs e) {
            if (MessageBox.Show("Are you sure you want to replace this colour?","Replace",MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            int filesUpdated = 0;
            //Regex regex = new Regex("(" + currentColor + ")[ ;]");
            String newColor = lblName.Text;
            foreach (String f in Directory.GetFiles(cssDir, "*.css"))
            {
                String fileName = f.Substring(0, f.LastIndexOf("."));
                String sFile = File.ReadAllText(fileName + ".css");
                if (Regex.Matches(sFile, currentColor + "[ ;]").Count > 0)
                {
                    sFile = Regex.Replace(sFile, currentColor + "(?<close>[ ;])", newColor + "${close}");
                    StreamWriter fCombined = File.CreateText(f);
                    fCombined.Write(sFile);
                    fCombined.Close();
                    fCombined.Dispose();
                    filesUpdated++;
                }
            }
            MessageBox.Show("Replaced color in " + filesUpdated + " files");
        }

    }
}
